<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
$msg            = "Record Not Found";
$partyArray     = array();
$itemArray      = array();
$sizeArray      = array();
$companyArray   = array();

if(isset($_POST['submit']))
{
	$partyId  = isset($_POST['partyId']) && ($_POST['partyId'] != "") ? $_POST['partyId'] : 0 ;
	$billDate = $_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay'];
	
  $insertBill = "INSERT INTO bill (partyId,billDate)
                            VALUE(".$partyId.",'".$billDate."')";
  $insertBillRes = mysql_query($insertBill);
  $lastBill = mysql_insert_id();
  if(!$insertBillRes)
  {
    die("Insert Query Not Inserted : ".mysql_error());
  }
  else
 {
    	$loopCntDetail = 0;
    	while($loopCntDetail < count($_POST['qualityId']))
    	{
    		$itemId      = isset($_POST['itemId'][$loopCntDetail]) ? $_POST['itemId'][$loopCntDetail] : 0;
    		$sizeId      = isset($_POST['sizeId'][$loopCntDetail]) ? $_POST['sizeId'][$loopCntDetail] : 0;
    		$companyId   = isset($_POST['companyId'][$loopCntDetail]) ? $_POST['companyId'][$loopCntDetail] : 0;
    		$qualityId   = isset($_POST['qualityId'][$loopCntDetail]) ? $_POST['qualityId'][$loopCntDetail] : 0;
    		$colorId     = isset($_POST['colorId'][$loopCntDetail]) ? $_POST['colorId'][$loopCntDetail] : 0;
    		$rate        = isset($_POST['rate'][$loopCntDetail]) ? $_POST['rate'][$loopCntDetail] : 0;
    		$amount      = isset($_POST['amount'][$loopCntDetail]) ? $_POST['amount'][$loopCntDetail] : 0;
    
        if($companyId > 0)
        {
	        $insertPartyDetail = "INSERT INTO billdetail (billId,itemId,sizeId,companyId,qualityId,colorId,rate,amount)
	                                           VALUES(".$lastBill.",".$itemId.",".$sizeId.",".$companyId.",".$qualityId.",
	                                                  ".$colorId.",".$rate.",".$amount.")";
	        $insertPartyDetailRes = mysql_query($insertPartyDetail);
	        if(!$insertPartyDetailRes)
	        {
	        	echo "data not Inserted ".mysql_error();
	        }
	        
	      }
	      $loopCntDetail++;
      }
       //header("location:toolPurchaseVr.php");
    }
}
$i = 0;
$selectParty = "SELECT partyId,partyName
                   FROM party
                   ORDER BY partyName";
$selectPartyResult = mysql_query($selectParty);
while($partyRow = mysql_fetch_array($selectPartyResult))
{
	$partyArray['partyId'][$i]     = $partyRow['partyId'];
	$partyArray['partyName'][$i]   = $partyRow['partyName'];
	$i++;
}

$k = 0;
$selectItem = "SELECT itemId,itemName
                   FROM item 
                   ORDER BY itemName";
$selectItemResult = mysql_query($selectItem);
while($itemRow = mysql_fetch_array($selectItemResult))
{
	$itemArray['itemId'][$k]     = $itemRow['itemId'];
	$itemArray['itemName'][$k]   = $itemRow['itemName'];
	$k++;
}


$m = 0;
$selectItem = "SELECT companyId,companyName
                   FROM company 
                   ORDER BY companyName";
$selectItemResult = mysql_query($selectItem);
while($itemRow = mysql_fetch_array($selectItemResult))
{
	$companyArray['companyId'][$m]     = $itemRow['companyId'];
	$companyArray['companyName'][$m]   = $itemRow['companyName'];
	$m++;
}
include("./bottom.php");
$smarty->assign("partyArray",$partyArray);
$smarty->assign("itemArray",$itemArray);
$smarty->assign("companyArray",$companyArray);
$smarty->assign("msg",$msg);
$smarty->display('bill.tpl');

?>